#include "uart.h"
#include "driver/uart.h"
#include "main.h"
#include "app_lvgl.h"


#define RX_BUF_SIZE  100

#define TXD_PIN GPIO_NUM_1
#define RXD_PIN GPIO_NUM_3

uint8_t uart_rx_buf[20];
uint8_t uart_rx_bufs[RX_BUF_SIZE];
uint16_t uart_rx_len;
uint16_t uart_rx_cnt;
uint16_t uart_rx_bytes;

static void uart_rx_task(void *pvParameters)
{
    while(1)
    {
        uart_rx_bytes = uart_read_bytes(UART_NUM_0, uart_rx_bufs, RX_BUF_SIZE, 1 / portTICK_RATE_MS);

        if(uart_rx_bytes>0)
        {

            //if(uart_rx_bytes>10)
            {
                //uart_rx_cnt=0;
                if(uart_rx_bufs[0]==0x5A
                &&
                uart_rx_bufs[1]==0xA5
                    &&
                uart_rx_bufs[8]==0x0d
                    &&
                uart_rx_bufs[9]==0x0a                      
                )
                {
                    //printf("-收到数据-\r\n");
                    mouse_x = uart_rx_bufs[2]<<8|uart_rx_bufs[3];
                    mouse_y = uart_rx_bufs[4]<<8|uart_rx_bufs[5];
                    mouse_sta = uart_rx_bufs[6]|uart_rx_bufs[7];

                    //lvgl_YouBiao_SheZhi_WeiZhi(mouse_x, mouse_y,mouse_sta);
                }   
            }
            uart_rx_bytes=0;
        }
    }
    
}

int uart_sendData(const char* logName, const char* data)
{
    const int len = strlen(data);
    const int txBytes = uart_write_bytes(UART_NUM_0, data, len);
    //ESP_LOGI(logName, "Wrote %d bytes", txBytes);
    return txBytes;
}

static void uart_tx_task(void *pvParameters)
{
    static const char *TX_TASK_TAG = "TX_TASK";
    esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO);
    while (1) {
        uart_sendData(TX_TASK_TAG, "Hello world\r\n");
        vTaskDelay(2000 / portTICK_PERIOD_MS);
    }
}


static void uart_rx_timercb(void *timer)
{
    uart_rx_bytes = uart_read_bytes(UART_NUM_0, uart_rx_bufs, RX_BUF_SIZE, 1 / portTICK_RATE_MS);

    if(uart_rx_bytes>0)
    {

         //if(uart_rx_bytes>10)
         {
            //uart_rx_cnt=0;
            if(uart_rx_bufs[0]==0x5A
            &&
            uart_rx_bufs[1]==0xA5
             &&
            uart_rx_bufs[8]==0x0d
             &&
            uart_rx_bufs[9]==0x0a                      
            )
            {
                //printf("-收到数据-\r\n");
                mouse_x = uart_rx_bufs[2]<<8|uart_rx_bufs[3];
                mouse_y = uart_rx_bufs[4]<<8|uart_rx_bufs[5];
                mouse_sta = uart_rx_bufs[6]|uart_rx_bufs[7];

                //lvgl_YouBiao_SheZhi_WeiZhi(mouse_x, mouse_y,mouse_sta);
            }   
         }
        uart_rx_bytes=0;
    }
}

uint8_t UART_init(void)
{
 
    esp_err_t ret=ESP_OK;
    const uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };
    uart_param_config(UART_NUM_0, &uart_config);
    uart_set_pin(UART_NUM_0, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

     ret = uart_driver_install(UART_NUM_0, RX_BUF_SIZE * 2, 0, 0, NULL, 0);

    if(ret!=ESP_OK)
    {
        UART_DEBUG("UART初始化失败\r\n");
        return ESP_FAIL;
    }else
    {
        UART_DEBUG("UART初始化成功\r\n");
    }
    uart_rx_len=0;
    uart_rx_cnt=0;
    uart_rx_bytes=0;

#if 0   
    esp_timer_create_args_t uart_rx_timer_conf = {
        .callback = uart_rx_timercb,
        .name     = "uart_rx_timer"
    };
    esp_timer_handle_t uart_rx_timer = NULL;
    esp_timer_create(&uart_rx_timer_conf, &uart_rx_timer);
    esp_timer_start_periodic(uart_rx_timer, 5 * 1000U);
#else
    xTaskCreate(uart_rx_task, "uart_rx_task", 1024*2, NULL, configMAX_PRIORITIES, NULL);
#endif


    
    //xTaskCreate(uart_tx_task, "uart_tx_task", 1024*2, NULL, configMAX_PRIORITIES-1, NULL);

 return ESP_OK;
}


















